import {createApp} from 'vue'
import App from '../App'
import Vuex from 'vuex'


createApp(App).use(Vuex)

export default new Vuex.Store({
	state: {
		token: sessionStorage.getItem('token'),
		userInfo: JSON.parse(localStorage.getItem('userInfo')),
		courseId: sessionStorage.getItem('courseId'),
		tagsList: [],
		menu: sessionStorage.getItem('menu'), // 用户权限栏
		collapse: false,
		article: sessionStorage.getItem('article'),
		category: sessionStorage.getItem('category'),
		room: localStorage.getItem('room'),
		region: localStorage.getItem('region'),
		bed: localStorage.getItem('bed')
	},
	mutations: {
		SET_COURSE_ID: (state, courseId) => {
			state.courseId = courseId
			sessionStorage.setItem('courseId', courseId)
		},
		SET_TOKEN: (state, token) => {
			state.token = token
			sessionStorage.setItem('token', token)
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo
			localStorage.setItem('userInfo', JSON.stringify(userInfo))
		},
		SET_MENU: (state, menu) => {
			sessionStorage.setItem('menu', JSON.stringify(menu));
		},
		REMOVE_INFO: (state) => {
			state.token = ''
			state.userInfo = {}
			localStorage.setItem('token', '')
			localStorage.setItem('userInfo', JSON.stringify(''))
		},
		SET_ARTICLE: (state, article) => {
			state.article = article
			sessionStorage.setItem('article', article)
		},
		SET_CATEGORY: (state, category) => {
			state.category = category
			sessionStorage.setItem('category', category)
		},
		SET_ROOM: (state, room) => {
			state.room = room
			localStorage.setItem('room', room)
		},
		SET_REGION: (state, region) => {
			state.region = region
			localStorage.setItem('region', region)
		},
		SET_BED: (state, bed) => {
			state.bed = bed
			localStorage.setItem('bed', bed)
		},
		//----------------------------------------------------------------
		delTagsItem(state, data) {
			state
				.tagsList
				.splice(data.index, 1);
		},
		setTagsItem(state, data) {
			state
				.tagsList
				.push(data)
		},
		clearTags(state) {
			state.tagsList = []
		},
		closeTagsOther(state, data) {
			state.tagsList = data;
		},
		closeCurrentTag(state, data) {
			for (let i = 0, len = state.tagsList.length; i < len; i++) {
				const item = state.tagsList[i];
				if (item.path === data.$route.fullPath) {
					if (i < len - 1) {
						data
							.$router
							.push(state.tagsList[i + 1].path);
					} else if (i > 0) {
						data
							.$router
							.push(state.tagsList[i - 1].path);
					} else {
						data
							.$router
							.push('/');
					}
					state
						.tagsList
						.splice(i, 1);
					break;
				}
			}
		},
		// 侧边栏折叠
		handleCollapse(state, data) {
			state.collapse = data;
		}
	},
	getters: {
		// get
		getUser: state => {
			return state.userInfo
		},
		getToken: state => {
			if (state.token === null) {
				return ''
			} else {
				return state.token
			}
		},
		getCourseId: state => {
			return state.courseId
		},
		getArticle: state => {
			return state.article
		},
		getCategory: state => {
			return state.category
		},
		getRoom: state => {
			if (state.room === null) {
				return ''
			} else {
				return state.room
			}
		},
		getRegion: state => {
			if (state.region === null) {
				return ''
			} else {
				return state.region
			}
		},
		getBed: state => {
			if (state.bed === null) {
				return ''
			} else {
				return state.bed
			}
		},

	},
	actions: {},
	modules: {}
})

